//
//  OrderForm_TCell.swift
//  MikerShop
//
//  Created by eme on 2016/12/19.
//  Copyright © 2016年 eme. All rights reserved.
//

/*
 owner:cy
 update:2016年12月19日18:56:04
 info: 进货单cell
 VC :  OrderForm_Vc
 */
import UIKit
import Kingfisher
import RxSwift
import UtilCore
import RickonCore

class OrderForm_Normal_TCell: UITableViewCell {
    
    @IBOutlet weak var seperateLine_V: UIView!
    
    @IBOutlet weak var ischeck_Btn: UIButton!
    
    @IBOutlet weak var img1_ImgV: UIImageView!
    
    @IBOutlet weak var pdname_Lb: UILabel!
    
    @IBOutlet weak var skuItems_Tv: UITableView!
    private let disposeBag = DisposeBag()
    
    var updateBtnState_Closure:((Void)-> Void)?
    var updateCartNum_Closure:((_ param:String)-> Void)?
    var item :OrderProductModel?{
        didSet{
            if let item = self.item {
                self.img1_ImgV.setUrlImage(item.img1)
                if item.istimeby == "1" {
                    self.pdname_Lb.setReplaceText(text: "\(AppConfig.timelimit)\(item.pdname)", replace: AppConfig.timelimit, value:Theme.btn.normal )
                }else{
                    self.pdname_Lb.text = item.pdname
                }
                self.products = item.array
                self.ischeck_Btn.isSelected = item.subHasSelected
            }
        }
    }
    
    var products = [OrderProductModel](){
        didSet{
            self.skuItems_Tv.reloadData()
        }
    }
    
    
    override func awakeFromNib() {
        self.seperateLine_V.backgroundColor = Theme.v.separate_Line
        self.ischeck_Btn
            .rx.tap
            .subscribe(onNext: { [unowned self] ( _ ) in
                self.item!.subHasSelected = !self.item!.subHasSelected
                if let updateBtnState_Closure = self.updateBtnState_Closure{
                    updateBtnState_Closure()
                }
            })
            .disposed(by: disposeBag)
        
    }
}
// MARK: UITableViewDelegate
extension OrderForm_Normal_TCell :UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return self.products.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCell(withIdentifier: "OrderForm_SkuItem_TCell", for: indexPath) as! OrderForm_SkuItem_TCell
        cell.item = self.products[indexPath.row]
        cell.updateBtnState_Closure = {[unowned self] _ in
            self.updateBtnState_Closure!()
        }
        cell.updateCartNum_Closure = {[unowned self] param in
            self.updateCartNum_Closure!(param)
        }
        
        return cell
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    
}
// MARK: UITableViewDelegate
extension OrderForm_Normal_TCell: UITableViewDelegate {
    /// 点击行事件
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return 85
    }
    
}

